package system;
import java.sql.*;
public class createAnswer{
	String studentid,testid;
	int true_or_false=0,grade=0;
	
	public void setStudentId(String sid){
		studentid=sid;
	}
	public String getStudentId(){
		return studentid;
	}
	
	public void setTestId(String tid){
		testid=tid;
	}
	public String getTestId(){
		return testid;
	}
	
	public int getSingleTrue(String questionid,String answer){
		String true_answer="";
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
	    rs=sql.executeQuery("select selection_true from single_selection where question_id='"+questionid+"'");
	    rs.next();
	    true_answer=rs.getString(1);
	    true_answer=true_answer.trim();
	    if(true_answer.equals(answer)){
	    	true_or_false=1;
	    	grade+=1;
	    }else true_or_false=0;
		con.close();
	    } catch(SQLException e1){}
	    return  true_or_false;
	}
	public boolean createSingle(String questionid,String answer){
		boolean  insert=false;
		String insertCondition="insert into student_question_single_selection values('"+studentid+"','"+testid+"','"+questionid+"','"+answer+"',"+getSingleTrue(questionid,answer)+")";
		Connection con;
	    Statement sql;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
    try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		int m=sql.executeUpdate(insertCondition);
		if(m!=0){
			insert=true;			
		}
		con.close();
	}
	catch(SQLException e1){insert=false;}
	return insert;
	}	
	
	public int getMultipleTrue(String questionid,String answer_A,String answer_B,String answer_C,String answer_D){
		String true_answer_A="",true_answer_B="",true_answer_C="",true_answer_D="";
		Connection con;
	    Statement sql;
	    ResultSet rs;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
        try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
	    rs=sql.executeQuery("select * from multiple_selection where question_id='"+questionid+"'");
	    rs.next();
	    
	    true_answer_A=rs.getString(7).trim();
	    true_answer_B=rs.getString(8).trim();
	    true_answer_C=rs.getString(9).trim();
	    true_answer_D=rs.getString(10).trim();
	    
	    if(answer_A==null){
	    	answer_A="null";
	    }
	    if(answer_B==null){
	    	answer_B="null";
	    }
	    if(answer_C==null){
	    	answer_C="null";
	    }
	    if(answer_D==null){
	    	answer_D="null";
	    }
	    
	    if(true_answer_A.equals(answer_A)&&true_answer_B.equals(answer_B)&&true_answer_C.equals(answer_C)&&true_answer_D.equals(answer_D)){
	    	true_or_false=1;
	    	grade+=2;
	    }else true_or_false=0;
		con.close();
	    } catch(SQLException e1){}
	    return  true_or_false;
	}
	public boolean createMultiple(String questionid,String answer_A,String answer_B,String answer_C,String answer_D){
		boolean  insert=false;
		String insertCondition="insert into student_question_multiple_selection values('"+studentid+"','"+testid+"','"+questionid+"','"+answer_A+"','"+answer_B+"','"+answer_C+"','"+answer_D+"',"+getMultipleTrue(questionid,answer_A,answer_B,answer_C,answer_D)+")";
		Connection con;
	    Statement sql;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
    try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		int m=sql.executeUpdate(insertCondition);
		if(m!=0){
			insert=true;			
		}
		con.close();
	}
	catch(SQLException e1){insert=false;}
	return insert;
	}
	
	public boolean createSubject(String questionid,String answer){
		boolean  insert=false;
		String insertCondition="insert into student_question_subjective_topic values('"+studentid+"','"+testid+"','"+questionid+"','"+answer+"',0)";
		Connection con;
	    Statement sql;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
    try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		int m=sql.executeUpdate(insertCondition);
		if(m!=0){
			insert=true;			
		}
		con.close();
	}
	catch(SQLException e1){insert=false;}
	return insert;
	}
	
	public boolean finishTest(){
		boolean  update=false;
		String updateCondition="update student_test set isFinished=1,grade="+grade+" where student_id='"+studentid+"' and test_id='"+testid+"'";
		Connection con;
	    Statement sql;
	    try{
	    	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	    	}
	    catch(Exception e){}
   
    try{
		String uri = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test_system";
		String user = "sa";
		String password = "sa";
		con=DriverManager.getConnection(uri,user,password);
		sql=con.createStatement();
		int m=sql.executeUpdate(updateCondition);
		if(m!=0){
			update=true;			
		}
		con.close();
	}
	catch(SQLException e1){update=false;}
	return update;
	}
}